fn main() {
  let num = 3;
  if num > 5 {
    println!("more than 5");
  } else {
    println!("small than 5");
  }
  // rust can not auto parse bool
  // if num {
  //   println!("check auto pass bool")
  // }

  //顺序检查，只会执行第一个满足条件的分支
  if num % 2 == 1 {
    println!("only first if can reach");
  } else if num % 3 == 0 {
    println!("can not be reach");
  } else {
    println!("can not be reach");
  }

  // if 返回值  三元表达式 a = b >5 ? 10 : 5
  let if_return = if num > 5 { 10 } else { 5 };
  println!("if return value {}", if_return);
  // if 表达式返回值必须是一致的
  // let if_return = if num > 5 { "more than 5" } else { 5 };

  // loop 需要if 来判断退出 P36
  let mut loop_count = 0;
  let result = loop {
    loop_count += 1;
    if loop_count == 10 {
      break loop_count + 1;
    }
  };
  println!("loop_count = {} result ={}", loop_count, result);

  //while 可以不需要if判断结束 P36
  let mut while_count = 0;
  while while_count != 10 {
    while_count += 1;
  }
  println!("while_count  = {}", while_count);

  //for 更适合用于遍历集合 P37

  let arr = [1, 2, 3, 4, 5, 6, 7];
  for e in arr.iter() {
    println!("array value = {}", e);
  }

  for num in (100..110).rev() {
    println!("get range = {}", num)
  }

  let match_value = 10;
  // 单模式匹配
  match match_value {
    1 => println!("1-5"),
    2 => println!("2"),
    _ => println!("not match"),
  }
  // 多个模式匹配
  match match_value {
    1 | 2 | 3 | 4 | 5 => println!("1-5"),
    6 | 7 | 8 | 9 => println!("2"),
    _ => println!("not match"),
  }

  // 返回匹配
  match match_value {
    1..=5 => println!("1-5"),
    6..=10 => println!("6-10"),
    _ => println!("not match"),
  }

  //解构结构体
  struct Point {
    x: f32,
    y: f32,
  }

  let p = Point { x: 0.1, y: 0.2 };
  let Point { x: a, y: b } = p;
  println!("x = {}", a);
  println!("y ={}", b);

  // 解构枚举

  enum WeekDay {
    Mon,
    Tues,
    Wed,
    Thur,
    Fri,
    Sat,
    Sun,
    Weather { e: f32, s: f32, w: f32, n: f32 },
  }

  let day = WeekDay::Weather {
    e: 1.1,
    s: 1.2,
    w: 1.3,
    n: 1.4,
  };
  match day {
    WeekDay::Mon => println!("mon day"),
    WeekDay::Tues => println!("tues day"),
    WeekDay::Wed => println!("wed day"),
    WeekDay::Thur => println!("thur day"),
    WeekDay::Fri => println!("fri day"),
    WeekDay::Sat => println!("sat day"),
    WeekDay::Sun => println!("sun day"),
    WeekDay::Weather { e, s, w, n } => {
      println!("e ={} s ={} w={} n ={}", e, s, w, n);
    }
  }

  //解构元组
  let ((x1, y1), Point { x, y }) = ((3.1, 10.1), Point { x: 3.1, y: -10.1 });
  println!("x1 ={} y1 ={} x ={} y ={}", x1, y1, x, y);

  // ..忽略剩余的值 _忽略部分值
  match p {
    Point { x, .. } => println!("only fetch x ={}", x),
  }

  let tuple = (1, 2, 3, 4, 5, 6, 7);
  match tuple {
    (first, .., last) => {
      println!("first ={} last ={}", first, last);
    }
  }

  // 匹配守卫提供的额外条件
  let num = Some(4);
  match num {
    Some(x) if x < 5 => println!("less than 5 {}", x),
    Some(x) if x > 5 => println!("more than 5 {}", x),
    Some(_) => println!("others"),
    None => println!("this is none"),
  }

  // @绑定
  let wd = WeekDay::Weather {
    e: 0.1,
    s: 0.2,
    w: 0.3,
    n: 0.4,
  };
  match wd {
    WeekDay::Weather {
      e: east @ 0.1..=1.1,
      s,
      w,
      n,
    } => {
      println!("found on east range : {}", east);
    },
    WeekDay::Weather {
      e,
      s: south @ 0.2..=1.2,
      w,
      n,
    } => {
      println!("found on south range :{}", south)
    },
    _ => println!("anything")
  }
}
